home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1988 / 04 / tracy / tracy.ls1
Text File  |  1979-12-31  |  5KB  |  115 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. SCR# 0
  8.  
  9. ( Corrected source screens)
  10.                                                                                                      The following four screens contain corrected source for the operators  GETDATA PUTDATA and PUTTEXT.  The original uncorrected versions are in the DDJ Forth column, Feb 1988.
  11.  
  12. Screen 5 defines GETDATA using
  13. BEGIN ... DUP IF  DROP ... THEN WHILE ... REPEAT
  14. instead of the preferred
  15. BEGIN ... WHILE ... WHILE ... REPEAT THEN                                                                                                                                                                                                                                                                                                                                                                                                                    
  16.  
  17. SCR# 1
  18.  
  19. \ Read BLOCKed file as data file
  20.  
  21.                                                                                                 : GETDATA ( a n - n2)
  22. \ reads n bytes of data from input file into address, n < 64K
  23. \ Returns n2 bytes not read ( ie beyond end of file ).
  24.     ( calculate # of bytes to move < 64K : )  POSITION 2@
  25.     BEGIN  2 PICK ( n ) DUP
  26.       IF ( n ) >R  2DUP 1K UM/MOD  SWAP DROP  1+  1K UM*
  27.          CAPACITY 2@ DMIN  2OVER D- 0= NOT OR  R> UMIN
  28.       THEN  ?DUP
  29.     WHILE  >R  2DUP 1K UM/MOD  BLOCK +  4 PICK R@ CMOVE
  30.      R@ 0 D+  2SWAP  R> /STRING  2SWAP
  31.     REPEAT  POSITION 2!  SWAP DROP ;
  32.  
  33. SCR# 2
  34.  
  35.                                                                                                                                                                                                                              \ Write BLOCKed file as data file
  36.                                                                                                : PUTDATA ( a n)
  37. \ writes n bytes of data to output file from address, n < 64K
  38.    ( extend the file as needed : )
  39.    DUP 0  POSITION 2@ D+  CAPACITY 2@  2OVER D-  DUP 0<
  40.    IF  2DUP DABS EXTEND  2OVER CAPACITY 2!  THEN  2DROP 2DROP
  41.    ( calculate # of bytes to move < 64K : )  POSITION 2@
  42.    BEGIN  2 PICK ( n ) DUP
  43.       IF ( n ) >R  2DUP 1K UM/MOD  SWAP DROP  1+  1K UM*
  44.          CAPACITY 2@ DMIN  2OVER D- 0= NOT OR  R> UMIN
  45.       THEN  ?DUP
  46.    WHILE  >R  2DUP 1K UM/MOD  BLOCK +  4 PICK SWAP R@ CMOVE
  47.       R@ 0 D+  2SWAP  R> /STRING  2SWAP   UPDATE
  48.    REPEAT  POSITION 2!  2DROP ;
  49.  
  50. SCR# 3
  51.                                                                                                                       
  52.  
  53.  0 \ Read text file with #EOF 
  54.  1
  55.  2 : GETTEXT ( a n - n2 f)   POSITION 2@   0 ( f ) >R
  56.  3 \ reads n bytes of text from input file into address, n < 64K
  57.  4 \ Returns n2 bytes not read ( ie end-of-line or beyond file)
  58.  5 \ Returns true if #EOL terminates line; false otherwise.
  59.  6    BEGIN  2DUP CAPACITY 2@ D<  3 PICK ( n ) AND
  60.  7    WHILE  2DUP 1K UM/MOD  BLOCK + C@ ( get ch)
  61.  8        DUP  #EOL =    DUP R> OR >R
  62.  9        OVER #EOF = OR NOT  AND ?DUP
  63. 10    WHILE  >R  1 0 D+  2SWAP  R> 2 PICK C!  1 /STRING  2SWAP
  64. 11    REPEAT THEN   2DUP CAPACITY 2@ D<
  65. 12    IF  2DUP 1K UM/MOD  BLOCK + C@  #EOL = DUP D-  THEN
  66. 13    2DUP CAPACITY 2@ D<
  67. 14    IF  2DUP 1K UM/MOD  BLOCK + C@  #LF  = DUP D-  THEN
  68. 15    POSITION 2!  SWAP DROP  R> ;
  69.  
  70.  
  71. SCR# 4
  72.  
  73.  0 \ Read text file without #EOF
  74.  1
  75.  2 : GETTEXT ( a n - n2 f)   POSITION 2@   0 ( f ) >R
  76.  3 \ reads n bytes of text from input file into address, n < 64K
  77.  4 \ Returns n2 bytes not read ( ie end-of-line or beyond file)
  78.  5 \ Returns true if #EOL terminates line; false otherwise.
  79.  6    BEGIN  2DUP CAPACITY 2@ D<  3 PICK ( n ) AND
  80.  7    WHILE  2DUP 1K UM/MOD  BLOCK + C@ ( get ch)
  81.  8       DUP  #EOL =    DUP R> OR >R
  82.  9                      NOT  AND ?DUP
  83. 10    WHILE  >R  1 0 D+  2SWAP  R> 2 PICK C!  1 /STRING  2SWAP
  84. 11    REPEAT THEN   2DUP CAPACITY 2@ D<
  85. 12    IF  2DUP 1K UM/MOD  BLOCK + C@  #EOL = DUP D-  THEN
  86. 13    2DUP CAPACITY 2@ D<
  87. 14    IF  2DUP 1K UM/MOD  BLOCK + C@  #LF  = DUP D-  THEN
  88. 15    POSITION 2!  SWAP DROP  R> ;
  89.  
  90.  
  91. SCR# 5
  92.  0 \ Read text file with #EOF
  93.  1
  94.  2 : GETTEXT ( a n - n2 f)   POSITION 2@   0 ( f ) >R
  95.  3 \ reads n bytes of text from input file into address, n < 64K
  96.  4 \ Returns n2 bytes not read ( ie end-of-line or beyond file)
  97.  5 \ Returns true if #EOL terminates line; false otherwise.
  98.  6    BEGIN  2DUP CAPACITY 2@ D<  3 PICK ( n ) AND 
  99.  7    DUP IF  DROP  2DUP 1K UM/MOD  BLOCK + C@ ( get ch)
  100.  8            DUP  #EOL =    DUP R> OR >R
  101.  9            OVER #EOF = OR NOT  AND ?DUP  THEN
  102. 10    WHILE  >R  1 0 D+  2SWAP  R> 2 PICK C!  1 /STRING  2SWAP
  103. 11    REPEAT THEN   2DUP CAPACITY 2@ D<
  104. 12    IF  2DUP 1K UM/MOD  BLOCK + C@  #EOL = DUP D-  THEN
  105. 13    2DUP CAPACITY 2@ D<
  106. 14    IF  2DUP 1K UM/MOD  BLOCK + C@  #LF  = DUP D-  THEN
  107. 15    POSITION 2!  SWAP DROP  R> ;
  108.  
  109.  
  110.  
  111.  
  112. -30-                                 
  113.  
  114.  
  115.